package com.example.lenovo.retrofit_test.data;

import android.content.Context;

import com.example.lenovo.retrofit_test.http.ApiService;
import com.google.gson.Gson;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * 初始化网络工具类,OkHttpClient   and   Retrofit
 * @Author LXY
 * Created at 2017/7/27 11:51
 */
public class HttpUtils {


    private static ApiService mApiService = null;

    public static ApiService getApiService(Context mContext) {


        if (mApiService == null) {

            OkHttpClient.Builder builder = new OkHttpClient.Builder();
            OkHttpClient okHttpClient= builder.addInterceptor(new CommonParamsInterceptor(mContext,new Gson()))
                    // 连接超时时间设置
                    .connectTimeout(10, TimeUnit.SECONDS)
                    // 读取超时时间设置
                    .readTimeout(10, TimeUnit.SECONDS)
                    .build();


            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(mApiService.BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    .client(okHttpClient)
                    .build();

            mApiService = retrofit.create(ApiService.class);
        }
        return mApiService;
    }

}


